package com.example.demo.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.constant.RedisConstant;
import com.example.demo.mapper.SysMenuMapper;
import com.example.demo.mapper.SysRoleMapper;
import com.example.demo.mapper.SysRoleMenuMapper;
import com.example.demo.mapper.entity.SysMenu;
import com.example.demo.mapper.entity.SysRole;
import com.example.demo.mapper.entity.SysRoleMenu;
import com.example.demo.service.SysMenuService;
import com.example.demo.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Lucy
 * @create 2024-02-03 21:05
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public List<SysMenu> buildMenuList(Integer parentId) {
            List<SysMenu> menus = null;
            int subMenuCount = 0;
            //顶级菜单的构建
            if(parentId == null){
                menus = sysMenuMapper.selectMenuList();
                //开始进行菜单的创建
                for(SysMenu menu : menus){
                    menu.setSubMenu(buildMenuList(menu.getMenuId())); //递归调用
                }
            } else{
                //子菜单构建 - 有parentId的情况
                menus = sysMenuMapper.selectMenuListByParentId(parentId);
                for(SysMenu menu : menus){
                    subMenuCount = sysMenuMapper.selectSubMenuCountByParentId(menu.getMenuId());
                    if (subMenuCount > 0) {
                        menu.setSubMenu(buildMenuList(menu.getMenuId()));
                    }
                }
            }
            return menus;
        }

    @Override
    public List<Integer> findMenuIdBySysRoleId(int roleId) {
        return sysMenuMapper.findMenuIdBySysRoleId(roleId);
    }

}
  /*
    //获取所有菜单列表-list<SysMenu>格式
    private List<SysMenu> getSysMenuList() {
        return sysMenuMapper.getSysMenuList();
    }
 */
